package com.raymond.cmpp.common.config;

import java.util.concurrent.*;

/**
 * 线程池配置类
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-08-05 11:39
 */
public class ThreadPoolConfig {
    private int corePoolSize = 20;

    private int maximumPoolSize = 20;

    private long keepAliveTime = 1;

    private TimeUnit unit = TimeUnit.MINUTES;

    private BlockingQueue<Runnable> workQueue = new LinkedBlockingDeque<Runnable>();

    private ThreadFactory threadFactory;

    public ThreadPoolConfig(String threadName) {
        this.threadFactory = r -> {
            Thread thread = new Thread(r);
            thread.setName(threadName + "-" + thread.getId());
            thread.setDaemon(true);
            return thread;
        };
    }

    private RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();

    public int getCorePoolSize() {
        return corePoolSize;
    }

    public void setCorePoolSize(int corePoolSize) {
        this.corePoolSize = corePoolSize;
    }

    public int getMaximumPoolSize() {
        return maximumPoolSize;
    }

    public void setMaximumPoolSize(int maximumPoolSize) {
        this.maximumPoolSize = maximumPoolSize;
    }

    public long getKeepAliveTime() {
        return keepAliveTime;
    }

    public void setKeepAliveTime(long keepAliveTime) {
        this.keepAliveTime = keepAliveTime;
    }

    public TimeUnit getUnit() {
        return unit;
    }

    public void setUnit(TimeUnit unit) {
        this.unit = unit;
    }

    public BlockingQueue<Runnable> getWorkQueue() {
        return workQueue;
    }

    public void setWorkQueue(BlockingQueue<Runnable> workQueue) {
        this.workQueue = workQueue;
    }

    public ThreadFactory getThreadFactory() {
        return threadFactory;
    }

    public void setThreadFactory(ThreadFactory threadFactory) {
        this.threadFactory = threadFactory;
    }

    public RejectedExecutionHandler getHandler() {
        return handler;
    }

    public void setHandler(RejectedExecutionHandler handler) {
        this.handler = handler;
    }
}
